package time;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.Test;

/**
 * 测试JDK8_时间.
 *
 * @author 宫新程
 * @since 2018/12/17 11:09 AM
 */
public class TestTime {

  private static final ThreadLocal<DateFormat> thread =
      ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));

  @Test
  public void test1() {
    LocalDate now = LocalDate.now();
    System.out.println(now); // 2018-12-17

    LocalTime localTime = LocalTime.now();
    System.out.println(localTime); // 11:14:31.110

    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTime1 = localDateTime.plusYears(2);
    System.out.println(localDateTime); // 2018-12-17T11:15:08.331
    System.out.println(localDateTime1); // 2020-12-17T12:44:08.539

    Instant instant = Instant.now();
    System.out.println(instant); // 2018-12-17T03:17:04.016Z  默认UTC时区.
    OffsetDateTime odt = instant.atOffset(ZoneOffset.ofHours(8));
    System.out.println(odt.toInstant().toEpochMilli()); // 1545022336216
  }

  /** 日期格式化. */
  @Test
  public void testDateFormat() {
    /** data -> String. */
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
    String format = formatter.format(LocalDateTime.now());
    System.out.println(format);

    /** String -> Date. */
    LocalDateTime parse = LocalDateTime.parse(format, formatter);
    System.out.println(parse);
  }

  /** 测试旧api线程安全问题. */
  @Test
  public void testThread() throws ExecutionException, InterruptedException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    /** 返回值. */
    Callable<Date> task =
        new Callable<Date>() {
          @Override
          public Date call() throws Exception {
            // return sdf.parse("20181207");   //存在线程安全问题.
            return thread.get().parse("20181207"); // 使用ThreadLocal解决线程安全问题.
          }
        };
    /** 创建线程池. */
    ExecutorService pool = Executors.newFixedThreadPool(10);
    /** 执行. */
    List<Future<Date>> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      Future<Date> submit = pool.submit(task);
      list.add(submit);
    }
    /** 遍历结果. */
    for (Future<Date> dateFuture : list) {
      System.out.println(dateFuture.get());
    }
    /** 关闭线程池. */
    pool.shutdown();
  }

  /** 新的时间api多线程. */
  @Test
  public void testThreadNew() throws ExecutionException, InterruptedException {
    /** 新的日期格式化. */
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
    /** Callable. */
    Callable<LocalDate> task =
        new Callable<LocalDate>() {
          @Override
          public LocalDate call() throws Exception {
            return LocalDate.parse("20181207", dtf);
          }
        };
    /** 线程池. */
    ExecutorService pool = Executors.newFixedThreadPool(10);
    /** 返回值. */
    List<Future<LocalDate>> list = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      Future<LocalDate> result = pool.submit(task);
      list.add(result);
    }
    /** 遍历结果. */
    for (Future<LocalDate> localDateFuture : list) {
      System.out.println(localDateFuture.get());
    }
    pool.shutdown();
  }

  @Test
  public void testThreadDemo() {

    Thread thread =
        new Thread() {
          @Override
          public void run() {
            System.out.println("Hello");
          }
        };
    thread.start();
  }
}
